package cn.yunyichina.provider.sb.service.impl;

import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.HisifaceService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.hisiface.shangbao.*;
import cn.yunyichina.provider.sb.constant.Constant;
import cn.yunyichina.provider.sb.dao.*;
import cn.yunyichina.provider.sb.entity.*;
import cn.yunyichina.provider.sb.util.Util;
import cn.yunyichina.provider.sb.vo.MedicalInhospitalFeeDetailVo;
import cn.yunyichina.provider.sb.vo.MedicalInhospitalFeeVo;
import cn.yunyichina.provider.sb.vo.SbRequest;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alipay.api.response.AlipayCommerceDataSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 15-AK on 2017/7/25.
 */
public class InhospitalFeeSeviceImpl {
    private static final Logger logger = LoggerFactory.getLogger(InhospitalFeeSeviceImpl.class);
    private MedicalInhospitalFeeDao medicalInhospitalFeeDao = SpringContextHandler.getBean("medicalInhospitalFeeDao");
    private MedicalInhospitalFeeDetailDao medicalInhospitalFeeDetailDao = SpringContextHandler.getBean("medicalInhospitalFeeDetailDao");
    private MedicalPatientDao medicalPatientDao = SpringContextHandler.getBean("medicalPatientDao");
    private MedicalInhospitalDiagnosisDao medicalInhospitalDiagnosisDao = SpringContextHandler.getBean("medicalInhospitalDiagnosisDao");
    private MedicalOperationRecordDao medicalOperationRecordDao = SpringContextHandler.getBean("medicalOperationRecordDao");
    private MedicalInhospitalReportidsDao medicalInhospitalReportidsDao = SpringContextHandler.getBean("medicalInhospitalReportidsDao");
    private MedicalExaminationDao medicalExaminationDao = SpringContextHandler.getBean("medicalExaminationDao");
    private MedicalCheckingDetailDao medicalCheckingDetailDao = SpringContextHandler.getBean("medicalCheckingDetailDao");
    private MedicalCheckingDao medicalCheckingDao = SpringContextHandler.getBean("medicalCheckingDao");


    private final static String HOSPITAL_CODE = "szszyy";

    public void executeUploadMedicalInhospitalFeeMessage() {
        try {
            MedicalInhospitalFee queryMedicalInhospitalFee = new MedicalInhospitalFee();
            queryMedicalInhospitalFee.setOrgCode(HOSPITAL_CODE);
            queryMedicalInhospitalFee.setUploadFlag(new Short("0"));
            List<MedicalInhospitalFee> medicalInhospitalFeeList = medicalInhospitalFeeDao.selectLimitFee(queryMedicalInhospitalFee);

            for (MedicalInhospitalFee medicalInhospitalFee : medicalInhospitalFeeList) {
                logger.info("inPatientId={},待上传住院费用信息medicalInhospitalFee={}", new Object[]{medicalInhospitalFee.getInpatientId(),JsonUtils.toJsonString(medicalInhospitalFee)});
                MedicalInhospitalFeeVo medicalInhospitalFeeVo = new MedicalInhospitalFeeVo();
                String encryptData = Util.encryptMedicalInhospitalFeeVo(medicalInhospitalFeeVo.buildMedicalInhospitalFeeVo(medicalInhospitalFee), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_INHOSPITAL_FEE);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("inPatientId={},住院费用信息上传支付宝响应参数 data ={}", new Object[]{medicalInhospitalFee.getInpatientId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("inPatientId={},住院费用信息上传成功",medicalInhospitalFee.getInpatientId());
                        medicalInhospitalFee.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("inPatientId={},住院费用信息上传失败",medicalInhospitalFee.getInpatientId());
                        medicalInhospitalFee.setExceptionMessage(response.getSubMsg());
                    }
                    medicalInhospitalFee.setUploadTimes(medicalInhospitalFee.getUploadTimes() + 1);
                    medicalInhospitalFeeDao.update(medicalInhospitalFee);

                } catch (Exception e) {
                    logger.error("住院费用信息上传系统异常", e);
                }
            }

        } catch (Exception e) {
            logger.error("住院费用信息上传系统异常", e);
        }
    }


    public void executeUploadMedicalInhospitalFeeDetailMessage() {
        try {
            MedicalInhospitalFeeDetail queryMedicalInhospitalFeeDetail = new MedicalInhospitalFeeDetail();
            queryMedicalInhospitalFeeDetail.setUploadFlag(new Short("0"));
            queryMedicalInhospitalFeeDetail.setOrgCode(HOSPITAL_CODE);
            List<MedicalInhospitalFeeDetail> medicalInhospitalFeeDetailList = medicalInhospitalFeeDetailDao.selectLimitFeeDetail(queryMedicalInhospitalFeeDetail);
            for (MedicalInhospitalFeeDetail medicalInhospitalFeeDetail : medicalInhospitalFeeDetailList) {
                logger.info("inPatientId={},待上传住院费用明细信息medicalInhospitalFeeDetail ={}", new Object[]{medicalInhospitalFeeDetail.getInpatientId(),JsonUtils.toJsonString(medicalInhospitalFeeDetail)});
                MedicalInhospitalFeeDetailVo medicalInhospitalFeeDetailVo = new MedicalInhospitalFeeDetailVo();
                String encryptData = Util.encryptMedicalInhospitalFeeDetailVo(medicalInhospitalFeeDetailVo.buildMedicalInhospitalFeeDetailVo(medicalInhospitalFeeDetail), Constant.ENCRYPT_KEY);
                SbRequest sbRequest = new SbRequest();
                sbRequest.setSceneCode(Constant.MEDICAL_INHOSPITAL_FEE_DETAIL);
                sbRequest.setEncryptData(encryptData);
                try {
                    AlipayCommerceDataSendResponse response = Util.executeUploadMessage(sbRequest);
                    logger.info("inPatientId={},住院费用明细上传支付宝响应参数 response ={}", new Object[]{medicalInhospitalFeeDetail.getInpatientId(),JsonUtils.toJsonString(response)});
                    if (response.isSuccess()) {
                        logger.info("inPatientId={},住院费用明细上传成功",medicalInhospitalFeeDetail.getInpatientId());
                        medicalInhospitalFeeDetail.setUploadFlag(new Short("1"));
                    } else {
                        logger.info("inPatientId={},住院费用明细上传失败",medicalInhospitalFeeDetail.getInpatientId());
                        medicalInhospitalFeeDetail.setExceptionMessage(response.getSubMsg());
                    }
                    medicalInhospitalFeeDetail.setUploadTimes(medicalInhospitalFeeDetail.getUploadTimes() + 1);
                    medicalInhospitalFeeDetailDao.update(medicalInhospitalFeeDetail);
                } catch (Exception e) {
                    logger.error("住院费用明细上传系统异常", e);
                }
            }

        } catch (Exception e) {
            logger.error("住院费用明细上传系统异常", e);
        }
    }


    public void executeDownloadInhospitalFeeInfo() {
        try {
            MedicalPatient queryMedicalPatient = new MedicalPatient();
            queryMedicalPatient.setOrgCode(HOSPITAL_CODE);
            queryMedicalPatient.setNotifyFlag(new Short("1"));
            queryMedicalPatient.setOutHospitalNotityFlag(new Short("1"));
            queryMedicalPatient.setBusinessType(new Short("2"));
            queryMedicalPatient.setDownLoadDiagnosisFlag(new Short("1"));
            queryMedicalPatient.setDownLoadFeeFlag(new Short("0"));
            List<MedicalPatient> medicalPatientList = medicalPatientDao.select(queryMedicalPatient);
            if (CollectionUtils.isNotEmpty(medicalPatientList)) {
                for (MedicalPatient medicalPatient : medicalPatientList) {
                    MedicalInhospitalDiagnosis queryMedicalInhospitalDiagnosis = new MedicalInhospitalDiagnosis();
                    queryMedicalInhospitalDiagnosis.setMedicalPatientId(medicalPatient.getId());
                    queryMedicalInhospitalDiagnosis.setCurrentOffset(2);
                    List<MedicalInhospitalDiagnosis> medicalInhospitalDiagnosisList = medicalInhospitalDiagnosisDao.select(queryMedicalInhospitalDiagnosis);
                    if (CollectionUtils.isNotEmpty(medicalInhospitalDiagnosisList)) {
                        MedicalInhospitalDiagnosis medicalInhospitalDiagnosis = medicalInhospitalDiagnosisList.get(0);
                        InPatientSumFeeListVo inPatientSumFeeListVo = new InPatientSumFeeListVo();
                        inPatientSumFeeListVo.setHospitalCode(HOSPITAL_CODE);
                        inPatientSumFeeListVo.setInPatientId(medicalPatient.getInpatientId());
                        inPatientSumFeeListVo.setBeginCostTime(medicalInhospitalDiagnosis.getInPatientTime());
                        inPatientSumFeeListVo.setEndCostTime(medicalInhospitalDiagnosis.getOutPatientTime());
                        logger.info("inPatientId={},获取商保住院总费用清单his入参inPatientSumFeeListVo={}", new Object[]{medicalPatient.getInpatientId(),JsonUtils.toJsonString(inPatientSumFeeListVo)});
                        ResCommon<InPatientSumFeeListVoResponse> hisRespose = HisifaceService.getHisifaceClient().getInPatientSumFeeList(inPatientSumFeeListVo);
                        logger.info("inPatientId={},获取商保住院总费用清单his出参hisRespose ={}", new Object[]{medicalPatient.getInpatientId(),JsonUtils.toJsonString(hisRespose)});
                        if (!"0".equals(hisRespose.getResultCode())) {
                            medicalPatient.setDownLoadFeeExceptionMessage(hisRespose.getResultMessage());
                            medicalPatientDao.update(medicalPatient);
                            continue;
                        }

                        InPatientSumFeeListVoResponse inPatientSumFeeListVoResponse = hisRespose.getResult();
                        if (inPatientSumFeeListVoResponse == null) {
                            medicalPatient.setDownLoadFeeExceptionMessage(hisRespose.getResultMessage());
                            medicalPatientDao.update(medicalPatient);
                            continue;
                        }

                        MedicalInhospitalFee medicalInhospitalFee = buildMedicalInhospitalFee(inPatientSumFeeListVoResponse);
                        medicalInhospitalFee.setTotalRows(1);
                        medicalInhospitalFee.setCurrentOffset(1);
                        medicalInhospitalFee.setUploadFlag(new Short("0"));
                        medicalInhospitalFee.setUploadTimes(0);
                        medicalInhospitalFee.setOrgCode(inPatientSumFeeListVo.getHospitalCode());
                        medicalInhospitalFee.setCaseNo(medicalInhospitalDiagnosis.getCaseNo());
                        medicalInhospitalFee.setMedicalPatientId(medicalInhospitalDiagnosis.getMedicalPatientId());
                        medicalInhospitalFeeDao.insert(medicalInhospitalFee);

                        List<InPatientSumFeeDetail> subItems = inPatientSumFeeListVoResponse.getSubItems();
                        if (CollectionUtils.isNotEmpty(subItems)) {
                            List<MedicalInhospitalFeeDetail> medicalInhospitalFeeDetails = new ArrayList<>();
                            int offset = 1;
                            for (InPatientSumFeeDetail inPatientFeeDetail : subItems) {
                                MedicalInhospitalFeeDetail medicalInhospitalFeeDetail = buildMedicalInhospitalFeeDetail(inPatientFeeDetail);
                                medicalInhospitalFeeDetail.setOrgCode(inPatientSumFeeListVo.getHospitalCode());
                                medicalInhospitalFeeDetail.setCaseNo(medicalInhospitalDiagnosis.getCaseNo());
                                medicalInhospitalFeeDetail.setUploadFlag(new Short("0"));
                                medicalInhospitalFeeDetail.setUploadTimes(0);
                                medicalInhospitalFeeDetail.setTotalRows(subItems.size());
                                medicalInhospitalFeeDetail.setCurrentOffset(offset++);
                                medicalInhospitalFeeDetail.setMedicalPatientId(medicalInhospitalDiagnosis.getMedicalPatientId());
                                medicalInhospitalFeeDetail.setMedicalInhospitalFeeId(medicalInhospitalFee.getId());
                                medicalInhospitalFeeDetail.setInpatientId(inPatientSumFeeListVoResponse.getInPatientId());
                                medicalInhospitalFeeDetails.add(medicalInhospitalFeeDetail);
                            }
                            medicalInhospitalFeeDetailDao.batchInsert(medicalInhospitalFeeDetails);
                        }
                        medicalPatient.setDownLoadFeeFlag(new Short("1"));
                        medicalPatient.setDownLoadFeeTimes(medicalPatient.getDownLoadFeeTimes() + 1);
                        medicalPatientDao.update(medicalPatient);

                    }
                }
            }

        } catch (Exception e) {
            logger.info("获取住院总清单异常", e);
        }

    }

    private MedicalInhospitalFee buildMedicalInhospitalFee(InPatientSumFeeListVoResponse inPatientSumFeeListVoResponse) {
        MedicalInhospitalFee medicalInhospitalFee = new MedicalInhospitalFee();
        medicalInhospitalFee.setId(PKGenerator.generateId());
        medicalInhospitalFee.setInpatientId(inPatientSumFeeListVoResponse.getInPatientId());
        medicalInhospitalFee.setRefundFeeId(inPatientSumFeeListVoResponse.getRefundFeeId());
        medicalInhospitalFee.setAdmissionNo(inPatientSumFeeListVoResponse.getAdmissionNo());
        medicalInhospitalFee.setSocialSecurityNo(inPatientSumFeeListVoResponse.getSocialSecurityNo());
        medicalInhospitalFee.setInsuranceType(inPatientSumFeeListVoResponse.getInsuranceType());
        medicalInhospitalFee.setInvoiceNo(inPatientSumFeeListVoResponse.getInvoiceNo());
        medicalInhospitalFee.setListType(conveterStringToShort(inPatientSumFeeListVoResponse.getListType()));
        medicalInhospitalFee.setMedicalFeeSettleCode(inPatientSumFeeListVoResponse.getMedicalFeeSettleCode());
        medicalInhospitalFee.setMedicalFeeSettleName(inPatientSumFeeListVoResponse.getMedicalFeeSettleName());
        medicalInhospitalFee.setInvoiceTotalAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getInvoiceTotalAmount()));
        medicalInhospitalFee.setSelfAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getSelfAmount()));
        medicalInhospitalFee.setLargeAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getLargeAmount()));
        medicalInhospitalFee.setFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getFundAmount()));
        medicalInhospitalFee.setPastYearPayAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getPastYearPayAmount()));
        medicalInhospitalFee.setCurYearPayAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getCurYearPayAmount()));
        medicalInhospitalFee.setCivilServantSubsidyAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getCivilServantSubsidyAmount()));
        medicalInhospitalFee.setPerBurdenAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getPerBurdenAmount()));
        medicalInhospitalFee.setCashAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getCashAmount()));
        medicalInhospitalFee.setCurYearBalance(conveterStringToInt(inPatientSumFeeListVoResponse.getCurYearBalance()));
        medicalInhospitalFee.setPastYearPayAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getPastYearPayAmount()));
        medicalInhospitalFee.setSeriousIllnessComplianceAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getSeriousIllnessComplianceAmount()));
        medicalInhospitalFee.setRescueFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getLamorFundAmount()));
        medicalInhospitalFee.setHealthFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getHealthFundAmount()));
        medicalInhospitalFee.setPromptMessage(inPatientSumFeeListVoResponse.getPromptMessage());
        medicalInhospitalFee.setOldWorkerFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getOldWorkerFundAmount()));
        medicalInhospitalFee.setHospitalCostAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getHospitalCostAmount()));
        medicalInhospitalFee.setOtherFundAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getOtherFundAmount()));
        medicalInhospitalFee.setCollectionTime(inPatientSumFeeListVoResponse.getCollectionTime());
        medicalInhospitalFee.setCollectionCompany(inPatientSumFeeListVoResponse.getCollectionCompany());
        medicalInhospitalFee.setCollector(inPatientSumFeeListVoResponse.getCollector());
        medicalInhospitalFee.setSelfCareAmount(conveterStringToInt(inPatientSumFeeListVoResponse.getSelfCareAmount()));
        medicalInhospitalFee.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalInhospitalFee;
    }

    private MedicalInhospitalFeeDetail buildMedicalInhospitalFeeDetail(InPatientSumFeeDetail inPatientFeeDetail) {
        MedicalInhospitalFeeDetail medicalInhospitalFeeDetail = new MedicalInhospitalFeeDetail();
        medicalInhospitalFeeDetail.setId(PKGenerator.generateId());
        medicalInhospitalFeeDetail.setFeeType(converterStringToShort(inPatientFeeDetail.getFeeType()));
        medicalInhospitalFeeDetail.setInPatientFeeDetailId(inPatientFeeDetail.getInPatientFeeDetailId());
        medicalInhospitalFeeDetail.setReportId(inPatientFeeDetail.getReportId());
        medicalInhospitalFeeDetail.setFeeItemName(inPatientFeeDetail.getFeeItemName());
        medicalInhospitalFeeDetail.setFeeItemCode(inPatientFeeDetail.getFeeItemCode());
        medicalInhospitalFeeDetail.setFeeItemUnit(inPatientFeeDetail.getFeeItemUnit());
        medicalInhospitalFeeDetail.setFeeItemSpec(inPatientFeeDetail.getFeeItemSpec());
        medicalInhospitalFeeDetail.setFeeItemPrice(conveterStringToInt(inPatientFeeDetail.getFeeItemPrice()));
        medicalInhospitalFeeDetail.setFeeItemQty(inPatientFeeDetail.getFeeItemQty());
        medicalInhospitalFeeDetail.setFeeItemAmount(conveterStringToInt(inPatientFeeDetail.getFeeItemAmount()));
        medicalInhospitalFeeDetail.setTotalAmount(conveterStringToInt(inPatientFeeDetail.getTotalAmount()));
        medicalInhospitalFeeDetail.setSelfBurdenPercentage(inPatientFeeDetail.getSelfBurdenPercentage() == null ? null : Double.valueOf(inPatientFeeDetail.getSelfBurdenPercentage()));
        medicalInhospitalFeeDetail.setItemSpec(inPatientFeeDetail.getItemSpec());
        medicalInhospitalFeeDetail.setItemType(inPatientFeeDetail.getItemType());
        medicalInhospitalFeeDetail.setItemUnit(inPatientFeeDetail.getItemUnit());
        medicalInhospitalFeeDetail.setItemOrigin(inPatientFeeDetail.getItemOrigin());
        medicalInhospitalFeeDetail.setPackageUnit(inPatientFeeDetail.getPackageUnit());
        medicalInhospitalFeeDetail.setMiniDosageUnit(inPatientFeeDetail.getMiniDosageUnit());
        medicalInhospitalFeeDetail.setPerDose(inPatientFeeDetail.getPerDose());
        medicalInhospitalFeeDetail.setDosageUnit(inPatientFeeDetail.getDosageUnit());
        medicalInhospitalFeeDetail.setPerDayTimes(converterStringToShort(inPatientFeeDetail.getPerDayTimes()));
        medicalInhospitalFeeDetail.setMedicineDays(converterStringToShort(inPatientFeeDetail.getMedicineDays()));
        medicalInhospitalFeeDetail.setSingleCompoundFlag(inPatientFeeDetail.getSingleCompoundFlag());
        medicalInhospitalFeeDetail.setChineseHerbalMedicineQty(converterStringToShort(inPatientFeeDetail.getChineseHerbalMedicineQty()));
        medicalInhospitalFeeDetail.setItemLimitPrice(conveterStringToInt(inPatientFeeDetail.getItemLimitPrice()));
        medicalInhospitalFeeDetail.setApprovalNo(inPatientFeeDetail.getApprovalNo());
        medicalInhospitalFeeDetail.setSelfFeeFlag(inPatientFeeDetail.getSelfFeeFlag());
        medicalInhospitalFeeDetail.setSpecialMedicineFlag(inPatientFeeDetail.getSpecialMedicineFlag());
        medicalInhospitalFeeDetail.setInsuranceAuxiliaryInfo(inPatientFeeDetail.getInsuranceAuxiliaryInfo());
        medicalInhospitalFeeDetail.setSingleQty(converterStringToShort(inPatientFeeDetail.getSingleQty()));
        medicalInhospitalFeeDetail.setFrequencyValue(conveterStringToInt(inPatientFeeDetail.getFrequencyValue()));
        medicalInhospitalFeeDetail.setMedicineMethod(inPatientFeeDetail.getMedicineMethod());
        medicalInhospitalFeeDetail.setFrequencyCode(inPatientFeeDetail.getFrequencyCode());
        medicalInhospitalFeeDetail.setItemCode(inPatientFeeDetail.getItemCode());
        medicalInhospitalFeeDetail.setDrugSn(inPatientFeeDetail.getDrugSn());
        medicalInhospitalFeeDetail.setDrugOrigin(inPatientFeeDetail.getDrugOrigin());
        medicalInhospitalFeeDetail.setDrugSpecQty(conveterStringToInt(inPatientFeeDetail.getDrugSpecQty()));
        medicalInhospitalFeeDetail.setItemExecDate(inPatientFeeDetail.getItemExecDate());
        medicalInhospitalFeeDetail.setDoctorCode(inPatientFeeDetail.getDoctorCode());
        medicalInhospitalFeeDetail.setDoctorName(inPatientFeeDetail.getDoctorName());
        medicalInhospitalFeeDetail.setCombination(inPatientFeeDetail.getCombination());
        medicalInhospitalFeeDetail.setFunctions(inPatientFeeDetail.getFunctions());
        medicalInhospitalFeeDetail.setContraindication(inPatientFeeDetail.getContraindication());
        medicalInhospitalFeeDetail.setRecipeFlag(inPatientFeeDetail.getRecipeFlag());
        medicalInhospitalFeeDetail.setRestrictionsFlag(inPatientFeeDetail.getRestrictionsFlag());
        medicalInhospitalFeeDetail.setRestrictions(inPatientFeeDetail.getRestrictions());
        medicalInhospitalFeeDetail.setBaseDrugIdentification(inPatientFeeDetail.getBaseDrugIdentification());
        medicalInhospitalFeeDetail.setNationalMedicineId(inPatientFeeDetail.getNationalMedicineId());
        medicalInhospitalFeeDetail.setRegId(inPatientFeeDetail.getRegId());
        medicalInhospitalFeeDetail.setBrand(inPatientFeeDetail.getBrand());
        medicalInhospitalFeeDetail.setItemEnglishName(inPatientFeeDetail.getItemEnglishName());
        medicalInhospitalFeeDetail.setInsuranceRange(inPatientFeeDetail.getInsuranceRange());
        medicalInhospitalFeeDetail.setMaxLimitPrice1(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice1()));
        medicalInhospitalFeeDetail.setMaxLimitPrice2(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice2()));
        medicalInhospitalFeeDetail.setMaxLimitPrice3(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice3()));
        medicalInhospitalFeeDetail.setMaxLimitPrice4(conveterStringToInt(inPatientFeeDetail.getMaxLimitPrice4()));
        medicalInhospitalFeeDetail.setInsuranceSettleRangeAmount(conveterStringToInt(inPatientFeeDetail.getInsuranceSettleRangeAmount()));
        medicalInhospitalFeeDetail.setManufacturer(inPatientFeeDetail.getManufacturer());
        medicalInhospitalFeeDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalInhospitalFeeDetail;
    }

    public void executeDwonLoadReports() {
        MedicalInhospitalReportids queryMedicalInhospitalReportids = new MedicalInhospitalReportids();
        queryMedicalInhospitalReportids.setOrgCode(HOSPITAL_CODE);
        List<MedicalInhospitalReportids> medicalInhospitalReportidsList = medicalInhospitalReportidsDao.selectRetryReport(queryMedicalInhospitalReportids);
        if (CollectionUtils.isNotEmpty(medicalInhospitalReportidsList)) {
            for (MedicalInhospitalReportids medicalInhospitalReportids : medicalInhospitalReportidsList) {
                if (1 == medicalInhospitalReportids.getReportType()) {
                    //1：检查报告
                    SBCheckRecordVo checkRecordVo = new SBCheckRecordVo();
                    checkRecordVo.setHospitalCode(medicalInhospitalReportids.getOrgCode());
                    checkRecordVo.setCheckId(medicalInhospitalReportids.getReportId());
                    logger.info("inPatientId={},获取住院商保检查信息his入参 checkRecordVo ={}", new Object[]{medicalInhospitalReportids.getInpatientId(),JsonUtils.toJsonString(checkRecordVo)});
                    ResCommon<SBCheckRecordVoResponse> hisfiaceSBCheckRecordResponse = HisifaceService.getHisifaceClient().getSBCheckRecord(checkRecordVo);
                    logger.info("inPatientId={},获取住院商保检查信息his出参 hisfiaceSBCheckRecordResponse ={}", new Object[]{medicalInhospitalReportids.getInpatientId(),JsonUtils.toJsonString(hisfiaceSBCheckRecordResponse)});
                    if (!"0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
                        medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getUploadTimes() + 1);
                        medicalInhospitalReportids.setExceptionMessage(hisfiaceSBCheckRecordResponse.getResultMessage());
                        medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                    }

                    if ("0".equals(hisfiaceSBCheckRecordResponse.getResultCode())) {
                        SBCheckRecordVoResponse sbCheckRecordVoResponse = hisfiaceSBCheckRecordResponse.getResult();
                        if (sbCheckRecordVoResponse != null) {
                            MedicalExamination examination = buildExamination(sbCheckRecordVoResponse);
                            examination.setOrgCode(medicalInhospitalReportids.getOrgCode());
                            examination.setMedicalPatientId(medicalInhospitalReportids.getMedicalPatientId());
                            examination.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.getId());
                            examination.setCaseNo(medicalInhospitalReportids.getCaseNo());

                            medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
                            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
                            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                            medicalExaminationDao.insert(examination);
                        }
                    }
                }

                if (2 == medicalInhospitalReportids.getReportType()) {
                    //通过reportId 查找对应的检验
                    MedicalChecking medicalChecking = null;
                    List<MedicalCheckingDetail> medicalCheckingDetails = null;
                    SBInspectRecordVo inspectRecordVo = new SBInspectRecordVo();
                    inspectRecordVo.setInspectId(medicalInhospitalReportids.getReportId());
                    inspectRecordVo.setHospitalCode(medicalInhospitalReportids.getOrgCode());
                    //拿到检验
                    logger.info("inPatientId={},获取商保检验信息his入参 inspectRecordVo ={}", new Object[]{medicalInhospitalReportids.getInpatientId(),JsonUtils.toJsonString(inspectRecordVo)});
                    ResCommon<SBInspectRecordVoResponse> hisfiaceSBInspectRecordResponse = HisifaceService.getHisifaceClient().getSBInspectRecord(inspectRecordVo);
                    logger.info("inPatientId={},获取商保检验信息his出参 hisfiaceSBInspectRecordResponse ={}", new Object[]{medicalInhospitalReportids.getInpatientId(),JsonUtils.toJsonString(hisfiaceSBInspectRecordResponse)});
                    if (!"0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
                        medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getUploadTimes() + 1);
                        medicalInhospitalReportids.setExceptionMessage(hisfiaceSBInspectRecordResponse.getResultMessage());
                        medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                    }

                    if ("0".equals(hisfiaceSBInspectRecordResponse.getResultCode())) {
                        SBInspectRecordVoResponse sBInspectRecordVoResponse = hisfiaceSBInspectRecordResponse.getResult();
                        if (sBInspectRecordVoResponse != null) {
                            medicalChecking = buildMedicalChecking(sBInspectRecordVoResponse);
                            medicalChecking.setMedicalPatientId(medicalInhospitalReportids.getMedicalPatientId());
                            medicalChecking.setCaseNo(medicalInhospitalReportids.getCaseNo());
                            medicalChecking.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.getId());

                            //患者信息id
                            List<SBInspectDetail> inspectDetails = sBInspectRecordVoResponse.getInspectDetails();
                            if (CollectionUtils.isNotEmpty(inspectDetails)) {
                                medicalCheckingDetails = new ArrayList<>();
                                for (SBInspectDetail sBInspectDetail : inspectDetails) {
                                    MedicalCheckingDetail medicalCheckingDetail = buildMedicalCheckingDetail(sBInspectDetail);
                                    medicalCheckingDetail.setBusinessFeeId(medicalChecking.getBusinessFeeId());
                                    medicalCheckingDetail.setInspectId(medicalChecking.getInspectId());
                                    medicalCheckingDetail.setBusinessType(converterStringToShort(sBInspectRecordVoResponse.getBusinessType()));
                                    medicalCheckingDetail.setOrgCode(medicalChecking.getOrgCode());
                                    medicalCheckingDetail.setMedicalCheckingId(medicalChecking.getId());
                                    medicalCheckingDetail.setMedicalPatientId(medicalChecking.getMedicalPatientId());
                                    medicalCheckingDetail.setCaseNo(medicalChecking.getCaseNo());
                                    medicalCheckingDetails.add(medicalCheckingDetail);
                                }
                            }
                            medicalCheckingDao.insert(medicalChecking);
                            if (CollectionUtils.isNotEmpty(medicalCheckingDetails)) {
                                medicalCheckingDetailDao.batchInsert(medicalCheckingDetails);
                            }
                            medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
                            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
                            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                        }
                    }
                }

                if (3 == medicalInhospitalReportids.getReportType()) {
                    //3:手术报告
                    SBSurgeryRecordVo surgeryRecordVo = new SBSurgeryRecordVo();
                    surgeryRecordVo.setBusinessType("2");
                    surgeryRecordVo.setHospitalCode(medicalInhospitalReportids.getOrgCode());
                    surgeryRecordVo.setSurgeryId(medicalInhospitalReportids.getReportId());
                    logger.info("inPatientId={},获取商保住院手术记录his入参 surgeryRecordVo ={}", new Object[]{medicalInhospitalReportids.getInpatientId(),JsonUtils.toJsonString(surgeryRecordVo)});
                    ResCommon<SBSurgeryRecordVoResponse> hisfiaceSBSurgeryRecordVoResponse = HisifaceService.getHisifaceClient().getSBSurgeryRecord(surgeryRecordVo);
                    logger.info("inPatientId={},获取商保住院手术记录his出参 hisfiaceSBSurgeryRecordVoResponse ={}", new Object[]{medicalInhospitalReportids.getInpatientId(),JsonUtils.toJsonString(hisfiaceSBSurgeryRecordVoResponse)});
                    if (!"0".equals(hisfiaceSBSurgeryRecordVoResponse.getResultCode())) {
                        medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getUploadTimes() + 1);
                        medicalInhospitalReportids.setExceptionMessage(hisfiaceSBSurgeryRecordVoResponse.getResultMessage());
                        medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                    }
                    if ("0".equals(hisfiaceSBSurgeryRecordVoResponse.getResultCode())) {
                        SBSurgeryRecordVoResponse sbSurgeryRecordVoResponse = hisfiaceSBSurgeryRecordVoResponse.getResult();
                        if (sbSurgeryRecordVoResponse != null) {
                            MedicalOperationRecord medicalOperationRecord = buildMedicalOperationRecord(sbSurgeryRecordVoResponse);

                            medicalOperationRecord.setMedicalPatientId(medicalInhospitalReportids.getMedicalPatientId());
                            medicalOperationRecord.setMedicalOutpatientFeeDetailId(medicalInhospitalReportids.getId());
                            medicalOperationRecord.setCaseNo(medicalInhospitalReportids.getCaseNo());
                            medicalOperationRecordDao.insert(medicalOperationRecord);
                            medicalInhospitalReportids.setDownLoadFlag(new Short("1"));
                            medicalInhospitalReportids.setDownLoadTimes(medicalInhospitalReportids.getDownLoadTimes() + 1);
                            medicalInhospitalReportidsDao.update(medicalInhospitalReportids);
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取住院的报告ids
     *
     * @param
     * @return
     */
    public void getSBInPatientReportIds() {

        MedicalPatient queryMedicalPatient = new MedicalPatient();
        queryMedicalPatient.setOrgCode(HOSPITAL_CODE);
        queryMedicalPatient.setNotifyFlag(new Short("1"));
        queryMedicalPatient.setOutHospitalNotityFlag(new Short("1"));
        queryMedicalPatient.setDownLoadReportIdFlag(new Short("0"));
        queryMedicalPatient.setBusinessType(new Short("2"));
        List<MedicalPatient> medicalPatientList = medicalPatientDao.select(queryMedicalPatient);
        logger.info("获取住院检验或检查或手术记录reportId 的就诊人信息 medicalPatientList={}", new Object[]{JsonUtils.toJsonString(medicalPatientList)});
        if (CollectionUtils.isNotEmpty(medicalPatientList)) {
            for (MedicalPatient medicalPatient : medicalPatientList) {

                MedicalInhospitalReportids queryMedicalInhospitalReportids = new MedicalInhospitalReportids();
                queryMedicalInhospitalReportids.setMedicalPatientId(medicalPatient.getId());
                List<MedicalInhospitalReportids> medicalInhospitalReportidses = medicalInhospitalReportidsDao.select(queryMedicalInhospitalReportids);
                if (CollectionUtils.isNotEmpty(medicalInhospitalReportidses)) {
                    continue;
                }
                SBInPatientReportIdsVo sbInPatientReportIdsVo = new SBInPatientReportIdsVo();
                sbInPatientReportIdsVo.setHospitalCode(medicalPatient.getOrgCode());
                sbInPatientReportIdsVo.setInPatientId(medicalPatient.getInpatientId());
                logger.info("请求调度中心获取住院检验和检查reportIds入参={}", new Object[]{JsonUtils.toJsonString(sbInPatientReportIdsVo)});
                ResCommon<SBInPatientReportIdsVoResponse> hisSBInPatientReportIdsVoResponse = HisifaceService.getHisifaceClient().getSBInPatientReportIds(sbInPatientReportIdsVo);
                logger.info("请求调度中心获取住院检验和检查reportIds出参={}", new Object[]{JsonUtils.toJsonString(hisSBInPatientReportIdsVoResponse)});
                if (!"0".equals(hisSBInPatientReportIdsVoResponse.getResultCode())) {
                    medicalPatient.setDownLoadReportIdTimes(medicalPatient.getDownLoadReportIdTimes() + 1);
                    medicalPatientDao.update(medicalPatient);
                    continue;
                }

                SBInPatientReportIdsVoResponse sBInPatientReportIdsVoResponse = hisSBInPatientReportIdsVoResponse.getResult();
                if (sBInPatientReportIdsVoResponse == null) {
                    medicalPatient.setDownLoadReportIdFlag(new Short("1"));
                    medicalPatient.setDownLoadReportIdTimes(medicalPatient.getDownLoadReportIdTimes() + 1);
                    medicalPatientDao.update(medicalPatient);
                    continue;
                }

                List<InPatientReportDetail> reportItems = sBInPatientReportIdsVoResponse.getReportItems();
                List<MedicalInhospitalReportids> medicalInhospitalReportidsList = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(reportItems)) {
                    int offset = 1;
                    for (InPatientReportDetail reportItem : reportItems) {
                        MedicalInhospitalReportids medicalInhospitalReportids = new MedicalInhospitalReportids();
                        medicalInhospitalReportids.setId(PKGenerator.generateId());
                        medicalInhospitalReportids.setMedicalPatientId(medicalPatient.getId());
                        medicalInhospitalReportids.setAdmissionNo(medicalPatient.getAdmissionNo());
                        medicalInhospitalReportids.setInTime(medicalPatient.getInTime());
                        medicalInhospitalReportids.setInpatientId(medicalPatient.getInpatientId());
                        medicalInhospitalReportids.setCaseNo(medicalPatient.getCaseNo());
                        medicalInhospitalReportids.setOrgCode(medicalPatient.getOrgCode());
                        medicalInhospitalReportids.setReportId(reportItem.getReportId());
                        medicalInhospitalReportids.setReportType(converterStringToShort(reportItem.getReportType()));
                        medicalInhospitalReportids.setCurrentOffset(offset++);
                        medicalInhospitalReportids.setTotalRows(reportItems.size());
                        medicalInhospitalReportids.setDownLoadFlag(new Short("0"));
                        medicalInhospitalReportids.setDownLoadTimes(0);
                        medicalInhospitalReportids.setUploadFlag(new Short("0"));
                        medicalInhospitalReportids.setUploadTimes(0);
                        medicalInhospitalReportids.setCreatedTime(DateUtils.dateToString(new Date()));
                        medicalInhospitalReportidsList.add(medicalInhospitalReportids);
                    }

                    if (CollectionUtils.isNotEmpty(medicalInhospitalReportidsList)) {
                        medicalInhospitalReportidsDao.batchInsert(medicalInhospitalReportidsList);
                        medicalPatient.setDownLoadReportIdFlag(new Short("1"));
                        medicalPatient.setDownLoadReportIdTimes(medicalPatient.getDownLoadReportIdTimes() + 1);
                        medicalPatientDao.update(medicalPatient);
                    }
                }
            }
        }
    }


    private MedicalExamination buildExamination(SBCheckRecordVoResponse sbCheckRecordVoResponse) {
        MedicalExamination medicalExamination = new MedicalExamination();
        medicalExamination.setId(PKGenerator.generateId());
        medicalExamination.setBusinessFeeId(sbCheckRecordVoResponse.getBusinessFeeId());
        medicalExamination.setBusinessType(converterStringToShort(sbCheckRecordVoResponse.getBusinessType()));
        medicalExamination.setCheckId(sbCheckRecordVoResponse.getCheckId());
        medicalExamination.setMzBillId(sbCheckRecordVoResponse.getMzBillId());
        medicalExamination.setAdmissionNo(sbCheckRecordVoResponse.getAdmissionNo());
        medicalExamination.setPatId(sbCheckRecordVoResponse.getPatId());
        medicalExamination.setSendDeptName(sbCheckRecordVoResponse.getSendDeptName());
        medicalExamination.setSendDoctorName(sbCheckRecordVoResponse.getSendDoctorName());
        medicalExamination.setSendDoctorName(sbCheckRecordVoResponse.getSendDoctorName());
        medicalExamination.setEmergencyCheckFlag(converterStringToShort(sbCheckRecordVoResponse.getEmergencyCheckFlag()));
        medicalExamination.setCheckDeptCode(sbCheckRecordVoResponse.getCheckDeptCode());
        medicalExamination.setCheckDeptName(sbCheckRecordVoResponse.getCheckDeptName());
        medicalExamination.setExecDoctorCode(sbCheckRecordVoResponse.getExecDoctorCode());
        medicalExamination.setExecDoctorName(sbCheckRecordVoResponse.getExecDoctorName());
        medicalExamination.setAuditDoctorName(sbCheckRecordVoResponse.getAuditDoctorName());
        medicalExamination.setAuditTime(sbCheckRecordVoResponse.getAuditTime());
        medicalExamination.setCheckType(sbCheckRecordVoResponse.getCheckType());
        medicalExamination.setCheckTime(sbCheckRecordVoResponse.getCheckTime());
        medicalExamination.setCheckMethod(sbCheckRecordVoResponse.getCheckMethod());
        medicalExamination.setCheckDesc(sbCheckRecordVoResponse.getCheckDesc());
        medicalExamination.setItemCode(sbCheckRecordVoResponse.getItemCode());
        medicalExamination.setChineseName(sbCheckRecordVoResponse.getChineseName());
        medicalExamination.setEnglishName(sbCheckRecordVoResponse.getEnglishName());
        medicalExamination.setCheckResultCode(sbCheckRecordVoResponse.getCheckResultCode());
        medicalExamination.setCheckResultDesc(sbCheckRecordVoResponse.getCheckResultDesc());
        medicalExamination.setImagingDesc(sbCheckRecordVoResponse.getImagingDesc());
        medicalExamination.setImagingDiagnosis(sbCheckRecordVoResponse.getImagingDiagnosis());
        medicalExamination.setReportDoctorCode(sbCheckRecordVoResponse.getReportDoctorCode());
        medicalExamination.setReportDoctorName(sbCheckRecordVoResponse.getReportDoctorName());
        medicalExamination.setReportTime(sbCheckRecordVoResponse.getReportTime());
        medicalExamination.setRisrelationNo(sbCheckRecordVoResponse.getRisRelationNo());
        medicalExamination.setImagingId(sbCheckRecordVoResponse.getImagingId());
        medicalExamination.setDeviceType(sbCheckRecordVoResponse.getDeviceType());
        medicalExamination.setClinicalDiagnosis(sbCheckRecordVoResponse.getClinicalDiagnosis());
        medicalExamination.setCheckPartCode(sbCheckRecordVoResponse.getCheckPartCode());
        medicalExamination.setCheckPart(sbCheckRecordVoResponse.getCheckPart());
        medicalExamination.setPictureNo(sbCheckRecordVoResponse.getPictureNo());
        medicalExamination.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalExamination;
    }

    private MedicalOperationRecord buildMedicalOperationRecord(SBSurgeryRecordVoResponse sbSurgeryRecordVoResponse) {
        MedicalOperationRecord medicalOperationRecord = new MedicalOperationRecord();
        medicalOperationRecord.setId(PKGenerator.generateId());
        medicalOperationRecord.setInpatientId(sbSurgeryRecordVoResponse.getInPatientId());
        medicalOperationRecord.setBusinessType(converterStringToShort(sbSurgeryRecordVoResponse.getBusinessType()));
        medicalOperationRecord.setPatId(sbSurgeryRecordVoResponse.getPatId());
        medicalOperationRecord.setAdmissionNo(sbSurgeryRecordVoResponse.getAdmissionNo());
        medicalOperationRecord.setInTime(conveterStringToInt(sbSurgeryRecordVoResponse.getInTime()));
        medicalOperationRecord.setMzFeeId(sbSurgeryRecordVoResponse.getMzFeeId());
        medicalOperationRecord.setSurgeryId(sbSurgeryRecordVoResponse.getSurgeryId());
        medicalOperationRecord.setSurgeryTime(sbSurgeryRecordVoResponse.getSurgeryTime());
        medicalOperationRecord.setPreDiagnosis(sbSurgeryRecordVoResponse.getPreDiagnosis());
        medicalOperationRecord.setSurgeryCode(sbSurgeryRecordVoResponse.getSurgeryCode());
        medicalOperationRecord.setSurgeryName(sbSurgeryRecordVoResponse.getSurgeryName());
        medicalOperationRecord.setSurgeryDoctor(sbSurgeryRecordVoResponse.getSurgeryDoctor());
        medicalOperationRecord.setAssistantName(sbSurgeryRecordVoResponse.getAssistantName());
        medicalOperationRecord.setAnaesthesiaMethod(sbSurgeryRecordVoResponse.getAnaesthesiaMethod());
        medicalOperationRecord.setProSurgeryManage(sbSurgeryRecordVoResponse.getProSurgeryManage());
        medicalOperationRecord.setElectronicMedicalRecords(sbSurgeryRecordVoResponse.getElectronicMedicalRecords());
        medicalOperationRecord.setItemSpec(sbSurgeryRecordVoResponse.getItemSpec());
        medicalOperationRecord.setItemType(sbSurgeryRecordVoResponse.getItemType());
        medicalOperationRecord.setItemUnit(sbSurgeryRecordVoResponse.getItemUnit());
        medicalOperationRecord.setItemOrigin(sbSurgeryRecordVoResponse.getItemOrigin());
        medicalOperationRecord.setPackageQty(converterStringToInt(sbSurgeryRecordVoResponse.getPackageQty()));
        medicalOperationRecord.setPackageUnit(sbSurgeryRecordVoResponse.getPackageUnit());
        medicalOperationRecord.setMiniDosageUnit(sbSurgeryRecordVoResponse.getMiniDosageUnit());
        medicalOperationRecord.setPerDose(sbSurgeryRecordVoResponse.getPerDose());
        medicalOperationRecord.setDosageUnit(sbSurgeryRecordVoResponse.getDosageUnit());
        medicalOperationRecord.setPerDayTimes(converterStringToInt(sbSurgeryRecordVoResponse.getPerDayTimes()));
        medicalOperationRecord.setMedicinedays(converterStringToInt(sbSurgeryRecordVoResponse.getMedicineDays()));
        medicalOperationRecord.setSingleCompoundFlag(sbSurgeryRecordVoResponse.getSingleCompoundFlag());
        medicalOperationRecord.setChineseHerbalMedicineQty(converterStringToShort(sbSurgeryRecordVoResponse.getChineseHerbalMedicineQty()));
        medicalOperationRecord.setItemLimitPrice(converterStringToInt(sbSurgeryRecordVoResponse.getChineseHerbalMedicineQty()));
        medicalOperationRecord.setApprovalNo(sbSurgeryRecordVoResponse.getApprovalNo());
        medicalOperationRecord.setSelfFeeFlag(sbSurgeryRecordVoResponse.getSelfFeeFlag());
        medicalOperationRecord.setSpecialMedicineFlag(sbSurgeryRecordVoResponse.getSpecialMedicineFlag());
        medicalOperationRecord.setInsuranceAuxiliaryInfo(sbSurgeryRecordVoResponse.getInsuranceAuxiliaryInfo());
        medicalOperationRecord.setSingleQty(converterStringToShort(sbSurgeryRecordVoResponse.getSingleQty()));
        medicalOperationRecord.setFrequencyValue(converterStringToInt(sbSurgeryRecordVoResponse.getFrequencyValue()));
        medicalOperationRecord.setMedicineMethod(sbSurgeryRecordVoResponse.getMedicineMethod());
        medicalOperationRecord.setFrequencyCode(sbSurgeryRecordVoResponse.getFrequencyCode());
        medicalOperationRecord.setItemCode(sbSurgeryRecordVoResponse.getItemCode());
        medicalOperationRecord.setDrugSn(sbSurgeryRecordVoResponse.getDrugSn());
        medicalOperationRecord.setDrugOrigin(sbSurgeryRecordVoResponse.getDrugOrigin());
        medicalOperationRecord.setDrugSpecOrigin(converterStringToInt(sbSurgeryRecordVoResponse.getDrugSpecOrigin()));
        medicalOperationRecord.setItemExecDate(sbSurgeryRecordVoResponse.getItemExecDate());
        medicalOperationRecord.setDoctorCode(sbSurgeryRecordVoResponse.getDoctorCode());
        medicalOperationRecord.setDoctorName(sbSurgeryRecordVoResponse.getDoctorName());
        medicalOperationRecord.setCombination(sbSurgeryRecordVoResponse.getCombination());
        medicalOperationRecord.setFunctions(sbSurgeryRecordVoResponse.getFunctions());
        medicalOperationRecord.setContraindication(sbSurgeryRecordVoResponse.getContraindication());
        medicalOperationRecord.setRecipeFlag(sbSurgeryRecordVoResponse.getRecipeFlag());
        medicalOperationRecord.setRestrictionsFlag(sbSurgeryRecordVoResponse.getRestrictionsFlag());
        medicalOperationRecord.setRestrictions(sbSurgeryRecordVoResponse.getRestrictions());
        medicalOperationRecord.setBaseDrugIdentification(sbSurgeryRecordVoResponse.getBaseDrugIdentification());
        medicalOperationRecord.setNationalMedicineId(sbSurgeryRecordVoResponse.getNationalMedicineId());
        medicalOperationRecord.setRegId(sbSurgeryRecordVoResponse.getRegId());
        medicalOperationRecord.setBrand(sbSurgeryRecordVoResponse.getBrand());
        medicalOperationRecord.setItemEnglishName(sbSurgeryRecordVoResponse.getItemEnglishName());
        medicalOperationRecord.setInsuranceRange(sbSurgeryRecordVoResponse.getInsuranceRange());
        medicalOperationRecord.setMaxLimitPrice1(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice1()));
        medicalOperationRecord.setMaxLimitPrice2(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice2()));
        medicalOperationRecord.setMaxLimitPrice3(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice3()));
        medicalOperationRecord.setMaxLimitPrice4(converterStringToInt(sbSurgeryRecordVoResponse.getMaxLimitPrice4()));
        medicalOperationRecord.setInsuranceSettleRangeAmount(converterStringToInt(sbSurgeryRecordVoResponse.getInsuranceSettleRangeAmount()));
        medicalOperationRecord.setManufacturer(sbSurgeryRecordVoResponse.getManufacturer());
        medicalOperationRecord.setCreatedTime(DateUtils.dateToString(new Date()));
        return medicalOperationRecord;
    }

    private MedicalChecking buildMedicalChecking(SBInspectRecordVoResponse sBInspectRecordVoResponse) {
        MedicalChecking medicalChecking = new MedicalChecking();
        medicalChecking.setId(PKGenerator.generateId());
        medicalChecking.setBusinessFeeId(sBInspectRecordVoResponse.getBusinessFeeId());
        medicalChecking.setBusinessType(converterStringToShort(sBInspectRecordVoResponse.getBusinessType()));
        medicalChecking.setInspectId(sBInspectRecordVoResponse.getInspectId());
        medicalChecking.setMzBillId(sBInspectRecordVoResponse.getMzBillId());
        medicalChecking.setAdmissionNo(sBInspectRecordVoResponse.getAdmissionNo());
        medicalChecking.setPatId(sBInspectRecordVoResponse.getPatId());
        medicalChecking.setSendHospitalCode(sBInspectRecordVoResponse.getSendHospitalCode());
        medicalChecking.setInspectTime(sBInspectRecordVoResponse.getInspectTime());
        medicalChecking.setSendDeptCode(sBInspectRecordVoResponse.getSendDeptCode());
        medicalChecking.setSendDeptName(sBInspectRecordVoResponse.getSendDeptName());
        medicalChecking.setSendDoctorCode(sBInspectRecordVoResponse.getSendDoctorCode());
        medicalChecking.setSendDoctorName(sBInspectRecordVoResponse.getSendDoctorName());
        medicalChecking.setInspectType(converterStringToShort(sBInspectRecordVoResponse.getInspectType()));
        medicalChecking.setInspectDeptCode(sBInspectRecordVoResponse.getInspectDeptCode());
        medicalChecking.setInspectDeptName(sBInspectRecordVoResponse.getSendDeptName());
        medicalChecking.setExecDoctorCode(sBInspectRecordVoResponse.getExecDoctorCode());
        medicalChecking.setExecDoctorName(sBInspectRecordVoResponse.getExecDoctorName());
        medicalChecking.setAuditDoctorCode(sBInspectRecordVoResponse.getAuditDoctorCode());
        medicalChecking.setAuditDoctorName(sBInspectRecordVoResponse.getAuditDoctorName());
        medicalChecking.setResultDesc(sBInspectRecordVoResponse.getResultDesc());
        medicalChecking.setCreatedTime(DateUtils.dateToString(new Date()));
        medicalChecking.setOrgCode(HOSPITAL_CODE);
        medicalChecking.setUploadFlag(new Short("0"));
        medicalChecking.setUploadTimes(0);
        return medicalChecking;
    }


    private MedicalCheckingDetail buildMedicalCheckingDetail(SBInspectDetail sBInspectDetail) {
        MedicalCheckingDetail medicalCheckingDetail = new MedicalCheckingDetail();
        medicalCheckingDetail.setId(PKGenerator.generateId());
        medicalCheckingDetail.setItemCode(sBInspectDetail.getItemCode());
        medicalCheckingDetail.setItemName(sBInspectDetail.getItemName());
        medicalCheckingDetail.setInspectMethod(sBInspectDetail.getInspectMethod());
        medicalCheckingDetail.setInspectDesc(sBInspectDetail.getInspectDesc());
        medicalCheckingDetail.setSampleType(sBInspectDetail.getSampleType());
        medicalCheckingDetail.setSampleCollectPart(sBInspectDetail.getSampleCollectPart());
        medicalCheckingDetail.setInspectResult(sBInspectDetail.getInspectResult());
        medicalCheckingDetail.setInspectDetailId(sBInspectDetail.getInspectDetailId());
        medicalCheckingDetail.setAuditTime(sBInspectDetail.getAuditTime());
        medicalCheckingDetail.setReportDoctorCode(sBInspectDetail.getReportDoctorCode());
        medicalCheckingDetail.setReportDoctorName(sBInspectDetail.getReportDoctorName());
        medicalCheckingDetail.setReportTime(sBInspectDetail.getReportTime());
        medicalCheckingDetail.setInspectResultCode(sBInspectDetail.getInspectResultCode());
        medicalCheckingDetail.setResultValue(sBInspectDetail.getResultValue());
        medicalCheckingDetail.setUnit(sBInspectDetail.getUnit());
        medicalCheckingDetail.setRefRange(sBInspectDetail.getRefRange());
        medicalCheckingDetail.setCreatedTime(DateUtils.dateToString(new Date()));
        medicalCheckingDetail.setOrgCode(HOSPITAL_CODE);
        medicalCheckingDetail.setUploadFlag(new Short("0"));
        medicalCheckingDetail.setUploadTimes(0);
        return medicalCheckingDetail;
    }


    public Short converterStringToShort(String value) {
        return value == null ? null : Short.valueOf(value);
    }

    public Integer converterStringToInt(String value) {
        return value == null ? null : converterStringToInt(value);
    }

    private Short conveterStringToShort(String value) {
        return value == null ? null : Short.valueOf(value);
    }

    private Integer conveterStringToInt(String value) {
        return value == null ? null : Integer.valueOf(value);
    }


}
